
package FinalProject;
import java.util.*;

public class SystemTest {
    private List<Employee> employees;

    public SystemTest() {
        this.employees = new ArrayList<>();
    }

    public static void main(String[] args) {
        SystemTest test = new SystemTest();
        test.initializeEmployees();
        test.testSalaryCalculation();
        test.testPaySlipGeneration();
        test.testExceptionHandling();
        test.testPolymorphism();
        test.displayStatistics();
    }

    private void initializeEmployees() {
        System.out.println("=== 初始化员工数据 ===\n");

        // 创建普通员工
        Ordinary emp1 = new Ordinary("张三", "E001", "技术部", 35.0);
        emp1.setHoursWorked(180);

        Ordinary emp2 = new Ordinary("李四", "E002", "销售部", 28.0);
        emp2.setHoursWorked(150);
        emp2.setOvertimeRate(2.0); // 设置更高的加班倍率

        // 创建管理人员
        Manager mgr1 = new Manager("王经理", "M001", "技术部", 12000.0);
        mgr1.setTeamSize(8);
        mgr1.setBonus(3000);

        Manager mgr2 = new Manager("赵总监", "M002", "销售部", 15000.0);
        mgr2.setTeamSize(12);
        mgr2.setBonus(5000);

        // 添加到员工列表
        employees.add(emp1);
        employees.add(emp2);
        employees.add(mgr1);
        employees.add(mgr2);

        // 显示员工信息
        for (Employee emp : employees) {
            System.out.println(emp);
        }
    }

    /**
     * 测试薪资计算
     */
    private void testSalaryCalculation() {
        System.out.println("\n=== 薪资计算测试 ===\n");

        for (Employee emp : employees) {
            try {
                double salary = emp.calculateSalary();
                System.out.printf("%s(%s) 的月薪: %.2f 元\n",
                        emp.getName(), emp.getId(), salary);
            } catch (WorkHourException e) {
                System.out.printf("❌ %s 薪资计算失败: %s\n",
                        emp.getName(), e.getMessage());
            }
        }
    }

    /**
     * 测试工资条生成
     */
    private void testPaySlipGeneration() {
        System.out.println("\n=== 工资条生成测试 ===\n");

        for (Employee emp : employees) {
            try {
                String paySlip = emp.generatePaySlip();
                System.out.println(paySlip);
            } catch (WorkHourException e) {
                System.out.printf("❌ %s 工资条生成失败: %s\n\n",
                        emp.getName(), e.getMessage());
            }
        }
    }

    /**
     * 测试异常处理
     */
    private void testExceptionHandling() {
        System.out.println("\n=== 异常处理测试 ===\n");

        // 测试负工作时长
        Ordinary errorEmp = new Ordinary("测试员工", "E999", "测试部", 25.0);
        errorEmp.setHoursWorked(-10);

        try {
            errorEmp.calculateSalary();
        } catch (WorkHourException e) {
            System.out.println("捕获异常: " + e.getMessage());
        }

        // 测试负奖金
        Manager errorMgr = new Manager("测试经理", "M999", "测试部", 8000.0);
        errorMgr.setBonus(-1000);

        try {
            errorMgr.calculateSalary();
        } catch (WorkHourException e) {
            System.out.println("捕获异常: " + e.getMessage());
        }
    }

    /**
     * 测试多态性
     */
    private void testPolymorphism() {
        System.out.println("\n=== 多态性测试 ===\n");

        for (Employee emp : employees) {
            System.out.printf("员工: %-8s | 类型: %-8s | 部门: %s\n",
                    emp.getName(), emp.getEmployeeType(), emp.getDepartment());

            // 多态调用
            try {
                double salary = emp.calculateSalary();
                System.out.printf("    月薪: %.2f 元\n", salary);
            } catch (WorkHourException e) {
                System.out.printf("    错误: %s\n", e.getMessage());
            }
        }
    }

    /**
     * 显示统计信息
     */
    private void displayStatistics() {
        System.out.println("\n=== 统计信息 ===\n");

        int ordinaryCount = 0;
        int managerCount = 0;
        double totalSalary = 0;

        for (Employee emp : employees) {
            if (emp instanceof Ordinary) {
                ordinaryCount++;
            } else if (emp instanceof Manager) {
                managerCount++;
            }

            try {
                totalSalary += emp.calculateSalary();
            } catch (WorkHourException e) {
                // 忽略异常员工
            }
        }

        System.out.printf("员工总数: %d 人\n", employees.size());
        System.out.printf("普通员工: %d 人\n", ordinaryCount);
        System.out.printf("管理人员: %d 人\n", managerCount);
        System.out.printf("月薪总额: %.2f 元\n", totalSalary);
        System.out.printf("平均月薪: %.2f 元\n", totalSalary / employees.size());
    }
}